<template>
  <el-dialog v-model="detailVisible" :destroy-on-close="true" title="详情" width="860" custom-class="no-header-dialog">
    <div class="modal-route-detail">
      <div class="modal-content">
        <el-tabs v-model="tabsMenuValue" @tab-change="tabChange">
          <el-tab-pane label="资产信息" name="A">
            <el-descriptions :column="2" border>
              <el-descriptions-item v-for="item in description.config" :label="item.label" :key="item.label">
                {{ description.data[item.value] }}
              </el-descriptions-item>
            </el-descriptions>
          </el-tab-pane>
          <el-tab-pane label="维护记录" name="B">
            <el-timeline style="max-width: 600px" v-if="timeLine.data.length > 0">
              <el-timeline-item v-for="(item, index) in timeLine.data" :key="index">
                <div v-for="(i, j) of timeLine.config" :key="j">
                  <span>{{ `${i.value}： ${item[i.key]}` }}</span>
                  <div v-if="i.key === 'content'" class="file-box">
                    <a
                      v-for="(file, index) in item['file_links']"
                      :key="index"
                      :href="file.link"
                      target="_blank"
                      :title="file.name"
                      class="file-link"
                    >
                      <el-icon><Link /></el-icon>{{ file.name }}
                    </a>
                  </div>
                </div>
              </el-timeline-item>
            </el-timeline>
            <el-empty description="无数据" v-else />
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>
    <template #footer>
      <div class="modal-footer">
        <el-button type="primary" @click="closeDialog">确 定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="detail">
import { ref } from "vue";
import { getDetail, getBaseDataList, getUpholdData } from "@/api/modules/asset";
const props = defineProps({
  data: {
    type: Object,
    required: false,
    default: () => ({})
  }
});
const detailVisible = ref<boolean>(false);
const tabsMenuValue = ref<string>("A");
const description = ref<any>({
  config: [],
  data: {},
  loading: false
});
const timeLine = ref<any>({
  config: [
    { key: "create_at", value: "操作时间" },
    { key: "user", value: "操作人员" },
    { key: "content", value: "操作记录" }
  ],
  data: [],
  loading: false
});
const tabChange = () => {
  switch (tabsMenuValue.value) {
    case "A":
      getDescriptionConfig();
      break;
    case "B":
      getUpholdList();
      break;
  }
};
// 获取资产信息数据
const getDescription = async () => {
  description.value.loading = true;
  const { data } = await getDetail(props.data.id);
  description.value.data = data;
  description.value.loading = false;
};
// 获取资产信息列表元素
const getDescriptionConfig = async () => {
  const { data } = await getBaseDataList();
  description.value.config = data.map(item => ({
    label: item.name,
    value: item.key,
    span: item.key === "other_info" ? 2 : 1
  }));
  description.value.config.unshift(
    ...[
      {
        label: "资源组类型",
        value: "group_name",
        span: 1
      },
      {
        label: "创建时间",
        value: "create_at",
        span: 1
      }
    ]
  );
  getDescription();
};
// 获取维护记录
const getUpholdList = async () => {
  timeLine.value.loading = true;
  const { data } = await getUpholdData(props.data.id);
  timeLine.value.data = data;
  timeLine.value.loading = false;
};
const openDialog = () => {
  detailVisible.value = true;
  tabsMenuValue.value = "A";
  timeLine.value.data = [];
  description.value.data = [];
  getDescriptionConfig();
};
const closeDialog = () => {
  detailVisible.value = false;
};
defineExpose({
  openDialog,
  closeDialog
});
</script>

<style lang="scss" scoped>
:deep(.ant-descriptions-item-content) {
  display: table-cell;
  font-size: 14px;
  font-weight: 600;
  line-height: 1.5;
  color: rgb(0 0 0 / 65%);
}
:deep(.ant-descriptions-item-label) {
  font-size: 14px;
  font-weight: 600;
  line-height: 1.5;
}
:deep(.ant-tabs-bar) {
  margin: 0 0 20px;
}
:deep(.ant-tabs-nav .ant-tabs-tab) {
  font-size: 16px;
}
.modal-content {
  display: flex;
  flex-direction: column;
  width: 100%;
  margin-bottom: 24px;
}
.card-span {
  margin: 10px;
  .label {
    display: inline-block;
    width: 120px;
    margin-right: 10px;
  }
  .itemText {
    display: inline-block;
  }
}
:deep(.el-dialog__header) {
  display: none;
}
.file-box {
  margin-left: 70px;
  .file-link {
    display: block;
    width: 100%;
    margin-top: 6px;
    overflow: hidden; /* 隐藏超出部分 */
    text-overflow: ellipsis;
    white-space: nowrap; /* 不换行 */
  }
}
</style>
